import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class SiteUserService extends BaseService<any> {
  private readonly logger = new Logger(SiteUserService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: SiteUserService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $search_model = (new SysUserRole())->order('is_admin desc,id desc')             ->with('userinfo')->append(['status_name'])             ->hasWhere('us...
const search_model = this.sysUserRoleService.order('is_admin desc,id desc')
            .with('userinfo').append(.status_name)
            .hasWhere('userinfo', function (query) use (where) {
                const condition = [];
                if (typeof where.username) && where.username !== '') condition[] = ['username', 'like', "%{this.model.handelSpecialCharacter(where.username)}%"];
                query.where(condition);
  }

  /**
   * getInfo
   * 对应 PHP: SiteUserService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(uid: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $info = (new SysUserRole())->where([['uid', '=', $uid], ['site_id', '=', $this->site_id]])             ->with('userinfo')->append(['status_name'])    ...
const info = this.sysUserRoleService.where([['uid', '=', uid], ['site_id', '=', this.site_id)))
            .with('userinfo').append(.status_name)
            .findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加当前站点用户
     * @param data
     * @return bool
     */
    async add(data)
    {
        return this.userService.addSiteUser(data, this.site_id);
    }

    /**
     * 编辑站点用户
     * @param int uid
     * @param data
     * @return true
     */
    async edit(int uid, data)
    {
        Db.startTrans(];
        try {
            this.userService.edit(uid, data];

            const role_ids = data.role_ids || [];
            //创建用户站点管理权限
            this.userRoleService.edit(this.site_id, uid, role_ids, data.status || UserDict.ON];

            Db.commit();
            return true;
  }

  /**
   * add
   * 对应 PHP: SiteUserService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: return (new UserService())->addSiteUser($data, $this->site_id);     }      /**      * 编辑站点用户      * @param int $uid      * @param array $data      * @...
return this.userService.addSiteUser(data, this.site_id);
    }

    /**
     * 编辑站点用户
     * @param int uid
     * @param data
     * @return true
     */
    async edit(int uid, data)
    {
        Db.startTrans(];
        try {
            this.userService.edit(uid, data];

            const role_ids = data.role_ids || [];
            //创建用户站点管理权限
            this.userRoleService.edit(this.site_id, uid, role_ids, data.status || UserDict.ON];

            Db.commit();
            return true;
  }

  /**
   * edit
   * 对应 PHP: SiteUserService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(uid: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: Db::startTrans();         try {             (new UserService())->edit($uid, $data);              $role_ids = $data['role_ids'] ?? [];             //创建...
Db.startTrans(];
        try {
            this.userService.edit(uid, data];

            const role_ids = data.role_ids || [];
            //创建用户站点管理权限
            this.userRoleService.edit(this.site_id, uid, role_ids, data.status || UserDict.ON];

            Db.commit();
            return true;
  }

  /**
   * modify
   * 对应 PHP: SiteUserService_admin::modify()
   * 逻辑类型: undefined - undefined
   */
  async modify(uid: number, field: number, data: number) {
    // 基于PHP真实逻辑: modify
    // PHP原文: $field_name = match ($field) {             'password' => 'password',             'real_name' => 'real_name',             'head_img' => 'head_img',...
const field_name = match (field) {
            password: 'password',
            real_name: 'real_name',
            head_img: 'head_img',
  }

  /**
   * del
   * 对应 PHP: SiteUserService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(uid: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: $where = [             ['uid', '=', $uid],             ['site_id', '=', $this->site_id]         ];         $user = (new SysUserRole())->where($where)-...
const where = [
            ['uid', '=', uid],
            ['site_id', '=', this.site_id]
        ];
        const user = this.sysUserRoleService.where(where).findOrEmpty(];
        if (user.isEmpty()) throw new BusinessException('USER_NOT_EXIST');
        if (user.is_admin) throw new BusinessException("SUPER_ADMIN_NOT_ALLOW_DEL"];
        user.delete(];
        LoginService.clearToken(uid];
        Cache.delete('user_role_list_' . uid);
        return true;
    }

    /**
     * 锁定
     * @param int uid
     * @return bool|true
     */
    async lock(int uid)
    {
        this.sysUserService.where([['uid', '=', uid))).update([status: UserDict.OFF)];
        this.sysUserRoleService.where([['uid', '=', uid], ['site_id', '=', this.site_id))).update([status: UserDict.OFF)];
        Cache.delete('user_role_list_' . uid];
        const site_id = this.request.defaultSiteId(];
        Cache.delete('user_role_'.uid + '_'.site_id];
        LoginService.clearToken(uid);
        return true;
    }

    /**
     * 解锁
     * @param int uid
     * @return bool|true
     */
    async unlock(int uid)
    {
        this.sysUserService.where([['uid', '=', uid))).update([status: UserDict.ON)];
        this.sysUserRoleService.where([['uid', '=', uid], ['site_id', '=', this.site_id))).update([status: UserDict.ON)];
        Cache.delete('user_role_list_' . uid];
        const site_id = this.request.defaultSiteId(];
        Cache.delete('user_role_'.uid + '_'.site_id];
        LoginService.clearToken(uid);
        return true;
    }
}
  }

  /**
   * lock
   * 对应 PHP: SiteUserService_admin::lock()
   * 逻辑类型: undefined - undefined
   */
  async lock(uid: number) {
    // 基于PHP真实逻辑: lock
    // PHP原文: (new SysUser())->where([['uid', '=', $uid]])->update(['status' => UserDict::OFF]);         (new SysUserRole())->where([['uid', '=', $uid], ['site_id',...
this.sysUserService.where([['uid', '=', uid))).update([status: UserDict.OFF)];
        this.sysUserRoleService.where([['uid', '=', uid], ['site_id', '=', this.site_id))).update([status: UserDict.OFF)];
        Cache.delete('user_role_list_' . uid];
        const site_id = this.request.defaultSiteId(];
        Cache.delete('user_role_'.uid + '_'.site_id];
        LoginService.clearToken(uid);
        return true;
    }

    /**
     * 解锁
     * @param int uid
     * @return bool|true
     */
    async unlock(int uid)
    {
        this.sysUserService.where([['uid', '=', uid))).update([status: UserDict.ON)];
        this.sysUserRoleService.where([['uid', '=', uid], ['site_id', '=', this.site_id))).update([status: UserDict.ON)];
        Cache.delete('user_role_list_' . uid];
        const site_id = this.request.defaultSiteId(];
        Cache.delete('user_role_'.uid + '_'.site_id];
        LoginService.clearToken(uid);
        return true;
    }
}
  }

  /**
   * unlock
   * 对应 PHP: SiteUserService_admin::unlock()
   * 逻辑类型: undefined - undefined
   */
  async unlock(uid: number) {
    // 基于PHP真实逻辑: unlock
    // PHP原文: (new SysUser())->where([['uid', '=', $uid]])->update(['status' => UserDict::ON]);         (new SysUserRole())->where([['uid', '=', $uid], ['site_id', ...
this.sysUserService.where([['uid', '=', uid))).update([status: UserDict.ON)];
        this.sysUserRoleService.where([['uid', '=', uid], ['site_id', '=', this.site_id))).update([status: UserDict.ON)];
        Cache.delete('user_role_list_' . uid];
        const site_id = this.request.defaultSiteId(];
        Cache.delete('user_role_'.uid + '_'.site_id];
        LoginService.clearToken(uid);
        return true;
    }
}
  }
}
